package com.yc.config;

import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.context.annotation.Bean;

/**
 * 配置负载均衡策略
 *
 * @author zwei
 * @create 2024-09-06 17:31
 */

public class MyLoadBalancerClientConfiguration {
//    //配置成轮询
//    @Bean
//    public ReactorServiceInstanceLoadBalancer customLoadBalancer(Environment environment,LoadBalancerClientFactory loadBalancerClientFactory ) {
//        String name=environment.getProperty(loadBalancerClientFactory.PROPERTY_NAME);
//        //轮询访问
//        return new RoundRobinLoadBalancer(loadBalancerClientFactory.getLazyProvider(name,ServiceInstanceListSupplier.class),name);
//    }


    //配置成随机访问
//    @Bean
//    public ReactorServiceInstanceLoadBalancer randowReactorServiceInstanceLoadBalancer(Environment environment, LoadBalancerClientFactory loadBalancerClientFactory){
//        String name=environment.getProperty(loadBalancerClientFactory.PROPERTY_NAME);
//        //随机访问
//        return new RandomLoadBalancer(loadBalancerClientFactory.getLazyProvider(name,ServiceInstanceListSupplier.class),name);
//    }

    @Bean
    public ReactorServiceInstanceLoadBalancer customLoadBalancer(ServiceInstanceListSupplier serviceInstanceListSupplier) {
        return new OddEvenTrafficLoadBalancer(serviceInstanceListSupplier);
    }
}
